package com.hepinghospital.user.ui;

import android.content.DialogInterface;
import android.content.Intent;
import android.view.KeyEvent;
import android.widget.Toast;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.ainemo.sdk.otf.NemoSDK;
import com.gyf.immersionbar.ImmersionBar;
import com.hepinghospital.user.ui.activity.consult.JiuZhenAddFragment;
import com.next.easynavigation.constant.Anim;
import com.next.easynavigation.view.EasyNavigationBar;
import com.hepinghospital.user.R;
import com.hepinghospital.user.base.BaseActivity;
import com.hepinghospital.user.fw_permission.FloatWinPermissionCompat;
import com.hepinghospital.user.service.WebSocketService;
import com.hepinghospital.user.ui.activity.consult.JiuZhenFragment;
import com.hepinghospital.user.ui.fragment.CheckRecordFragment;
import com.hepinghospital.user.ui.fragment.HomeFragment;
import com.hepinghospital.user.ui.fragment.MineFragment1;
import com.hepinghospital.user.ui.xylink.XyCallActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends BaseActivity {

    private String[] tabText = {"首页", "就诊室", "检查报告", "个人中心"};

    /***未选中icon*/
    private final int[] normalIcon = {R.drawable.ic_home_normal, R.drawable.ic_im_normal, R.drawable.ic_check_normal, R.drawable.ic_mine_normal};

    /***选中时icon*/
    private final int[] selectIcon = {R.drawable.ic_home, R.drawable.ic_im, R.drawable.ic_check, R.drawable.ic_mine};

    private List<Fragment> fragments = new ArrayList<>();
    private long firstTime = 0;
    private EasyNavigationBar navigationBar;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_main;
    }


    @Override
    protected void initViews() {
        navigationBar = findViewById(R.id.navigationBar);
        fragments.add(HomeFragment.newInstance());
        fragments.add(JiuZhenAddFragment.newInstance());
        fragments.add(CheckRecordFragment.newInstance());
        fragments.add(MineFragment1.newInstance());

        navigationBar.titleItems(tabText)
                .normalIconItems(normalIcon)
                .selectIconItems(selectIcon)
                .fragmentList(fragments)
                .fragmentManager(getSupportFragmentManager())
                .iconSize(30)
                .tabTextSize(12)
//                .tabTextTop(2)
                .normalTextColor(ContextCompat.getColor(mContext, R.color.lightBlack))
                .selectTextColor(ContextCompat.getColor(mContext, R.color.color_4d7cfa))
                .canScroll(false)
                .anim(Anim.Pulse)
                .build();


        navigationBar.onTabClickListener((view, position) -> {
            if (position == 0 || position == 3) {
                ImmersionBar.with(mContext).fitsSystemWindows(false).transparentStatusBar().statusBarDarkFont(true).navigationBarColor(R.color.black).autoDarkModeEnable(true).init();
            } else {
                ImmersionBar.with(mContext).fitsSystemWindows(true).statusBarColor(R.color.white).navigationBarColor(R.color.black).autoDarkModeEnable(true).init();
            }
            return false;
        });
    }

    @Override
    protected void doBeforeSetcontentView() {
        ImmersionBar.with(mContext).fitsSystemWindows(false).transparentStatusBar().statusBarDarkFont(true).navigationBarColor(R.color.black).autoDarkModeEnable(true).init();
    }

    @Override
    protected void initData() {
        NemoSDK.getInstance().setNemoReceivedCallListener((name, number, callIndex) -> {
            Intent callIntent = new Intent(mContext, XyCallActivity.class);
            callIntent.putExtra("name", name);
            callIntent.putExtra("callIndex", callIndex);
            startActivity(callIntent);
        });
    }

    @Override
    protected void onDestroy() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        super.onDestroy();
    }


    public EasyNavigationBar getNavigationBar() {
        return navigationBar;
    }


    public int dip2px(float dpValue) {
        final float scale = getApplication().getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }


    protected void checkPermissionAndShow() {
        // 检查是否已经授权
        if (FloatWinPermissionCompat.getInstance().check(this)) {
        } else {
            // 授权提示
            new androidx.appcompat.app.AlertDialog.Builder(this).setTitle("悬浮窗权限未开启")
                    .setMessage("你的手机没有授权" + this.getString(R.string.app_name) + "获得悬浮窗权限，视频功能将无法正常使用")
                    .setPositiveButton("开启", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // 显示授权界面
                            try {
                                FloatWinPermissionCompat.getInstance().apply(mContext);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    })
                    .setNegativeButton("取消", null).show();
        }
    }


    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
            long secondTime = System.currentTimeMillis();
            if (secondTime - firstTime > 1000) {
                Toast.makeText(mContext, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                firstTime = secondTime;
                return true;
            } else {
                stopService(new Intent(this, WebSocketService.class));
                System.exit(0);
            }
        }
        return super.onKeyUp(keyCode, event);
    }
}
